package com.hsh.gaoqianlianmeng.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {
    /**
     * 读取文件
     *
     * @param filePath
     * @return
     */

    public static StringBuilder readFile(String filePath) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            sb.append("\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb;
    }

    /**
     * 写入文件
     *
     * @param filePath
     * @param str
     * @return
     */
    public static boolean writeFile(String filePath, String str) {
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(str);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
